/*
 * Copyright (c) 2018. J4dream Created.
 */

package com.j4dream.property.job.impl;

import com.google.common.collect.ImmutableList;
import com.j4dream.property.job.DBWriter;
import com.j4dream.property.job.FileJob;
import com.j4dream.property.job.CsvItemReader;
import jdk.nashorn.internal.ir.annotations.Immutable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
@Slf4j
public class ImportFileJobImpl implements FileJob {

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private DBWriter writer;

    @Override
    public void executeJob(String path) {
        CsvItemReader reader = new CsvItemReader(ImmutableList.of("243", "fsdf"));
        reader.setName("READ_CSV");
        Step step1 = stepBuilderFactory
                .get("MOCK_DATA_" + UUID.randomUUID())
                .chunk(10)
                .reader(reader)
                .writer(writer)
                .build();


        Job jb =jobBuilderFactory.get("TEST_" + UUID.randomUUID())
                .incrementer(new RunIdIncrementer())
                .start(step1)
                .build();
        try {
            jobLauncher.run(jb, new JobParameters());
        } catch (Exception ex) {
            log.info(ex.getMessage().toString());
        }
    }


}
